﻿using Hospital.Utility.ApiResult;
using Microsoft.AspNetCore.Mvc;
using Microsoft.IdentityModel.Tokens;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using System.Text;
using Hospital.Utility._MD5;
using Hospital.IService;
using Hospital.Model.DTO;

namespace Hospital.JWT.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class AuthorizeController(IUserInfoService iUserInfoService,IPatientInfoService iPatientInfoService) : ControllerBase
    {
        private readonly IUserInfoService iUserInfoService = iUserInfoService;
        private readonly IPatientInfoService iPatientInfoService = iPatientInfoService;
        [HttpPost("login")]
        public async Task<ApiResult> DoctorLogin([FromBody] UserInfoDTO userInfoDTO)
        {
            //加密后的密码
            string pwd = MD5Helper.MD5Encrypt32(userInfoDTO.Password);
            //数据校验
            var userInfo = await iUserInfoService.FindAsync(c => c.Account == userInfoDTO.Account && c.Password == pwd);
            if (userInfo == null) return ApiResultHelper.Error("登录失败");
            //登录成功,jwt验证
            var claims = new Claim[]
            {
                new("Id",userInfo.Id.ToString()), //存储用户的id
                new("Account",userInfo.Account ?? string.Empty),
                //不可存放密码,这里会被解密
            };
            var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes("SDMC-CJAS1-SAD-DFSFA-SADHJVF-VF-EAMF"));
            //issuer代表颁发Token的web应用程序,audience是Token的受益者
            var token = new JwtSecurityToken(
                issuer: "http://localhost:5131",
                audience: "http://localhost:5076",
                claims: claims,
                notBefore: DateTime.Now,
                expires: DateTime.Now.AddHours(1),//添加token有效期,notBefore---expires
                signingCredentials: new SigningCredentials(key, SecurityAlgorithms.HmacSha256)
                );
            var jwtToken = new JwtSecurityTokenHandler().WriteToken(token);//写入token生成字符串
            return ApiResultHelper.Success(new JwtToken { Token = jwtToken });
        }

        //患者登录
        [HttpPost("PatientLogin")]
        public async Task<ApiResult> PatientLogin(string phone,string password)
        {
            //加密后的密码
            string pwd = MD5Helper.MD5Encrypt32(password);
            //数据校验
            var patientInfo = await iPatientInfoService.FindAsync(c => c.Phone == phone && c.Password == pwd);
            if (patientInfo == null) return ApiResultHelper.Error("登录失败");
            //登录成功,jwt验证
            var claims = new Claim[]
            {
                new("Id",patientInfo.Id.ToString()), //存储患者的id
                new("Phone",patientInfo.Phone ?? string.Empty),
                //不可存放密码,这里会被解密
            };
            var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes("SDMC-CJAS1-SAD-DFSFA-SADHJVF-VF-EAMF"));
            //issuer代表颁发Token的web应用程序,audience是Token的受益者
            var token = new JwtSecurityToken(
                issuer: "http://localhost:5131",
                audience: "http://localhost:5076",
                claims: claims,
                notBefore: DateTime.Now,
                expires: DateTime.Now.AddHours(1),//添加token有效期,notBefore---expires
                signingCredentials: new SigningCredentials(key, SecurityAlgorithms.HmacSha256)
                );
            var jwtToken = new JwtSecurityTokenHandler().WriteToken(token);//写入token生成字符串
            return ApiResultHelper.Success(new JwtToken { Token = jwtToken });
        }
    }

    public class JwtToken
    {
        public required string Token { get; set; }
    }
}
